package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacTestWorkFlowNode;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.mapper.bac.BacTestWorkFlowNodeMapper;
import com.ysd.lis.service.bac.BacTestWorkFlowNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 微生物检验应用流程方案节点 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-11
 */
@Service
public class BacTestWorkFlowNodeServiceImpl extends ServiceImpl<BacTestWorkFlowNodeMapper, BacTestWorkFlowNode> implements BacTestWorkFlowNodeService {
    @Autowired
    BacTestWorkFlowNodeMapper bacTestWorkFlowNodeMapper;

    @Override
    public Result findBacTestWorkFlowNodeList(BacTestWorkFlowNode bacTestWorkFlowNode) {
        List<BacTestWorkFlowNode> list = findBacTestWorkFlowNodeArray(bacTestWorkFlowNode);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacTestWorkFlowNode> findBacTestWorkFlowNodeArray(BacTestWorkFlowNode bacTestWorkFlowNode) {
        MPJLambdaWrapper<BacTestWorkFlowNode> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacTestWorkFlowNode::getDelFlag, 0);
        //改不了
        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlowNode.getTestFlowNo()), BacTestWorkFlowNode::getTestFlowNo, bacTestWorkFlowNode.getTestFlowNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacTestWorkFlowNode.getNodeNo()), BacTestWorkFlowNode::getNodeNo, bacTestWorkFlowNode.getNodeNo());

        /*需要加左连接 链接工作流程节点   左连接节点字典*/

        return bacTestWorkFlowNodeMapper.selectList(queryWrapper);
    }

    @Override
    public BacTestWorkFlowNode findBacTestWorkFlowNodeByLast(LabMaininfo mainInfo) {
        MPJLambdaWrapper<BacTestWorkFlowNode> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacTestWorkFlowNode::getRptGroup, mainInfo.getRptGroup());
        queryWrapper.eq(BacTestWorkFlowNode::getSampleda, mainInfo.getSampleda());
        queryWrapper.eq(BacTestWorkFlowNode::getSampleNo, mainInfo.getSampleNo());
        queryWrapper.eq(BacTestWorkFlowNode::getDelFlag, 0);
        queryWrapper.orderByDesc(BacTestWorkFlowNode::getSeq);
        List<BacTestWorkFlowNode> bacTestWorkFlowNodes = bacTestWorkFlowNodeMapper.selectList(queryWrapper);
        return bacTestWorkFlowNodes.size() > 0 ? bacTestWorkFlowNodes.get(0) : null;
    }

    @Override
    public Result addBacTestWorkFlowNode(BacTestWorkFlowNode bacTestWorkFlowNode) {
        int i = bacTestWorkFlowNodeMapper.insert(bacTestWorkFlowNode);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacTestWorkFlowNode);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacTestWorkFlowNode(BacTestWorkFlowNode bacTestWorkFlowNode) {
        bacTestWorkFlowNode.setDelFlag(1);
        int i = bacTestWorkFlowNodeMapper.updateById(bacTestWorkFlowNode);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacTestWorkFlowNode);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacTestWorkFlowNode(BacTestWorkFlowNode bacTestWorkFlowNode) {
        int i = bacTestWorkFlowNodeMapper.updateById(bacTestWorkFlowNode);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacTestWorkFlowNode);
        }
        return Result.fail("更新失败");
    }
}
